import init from '@feidao/web/framework/browser/d/component';
import { id, spaceid, systemid, refsystemid } from './config';
import { IFeidaoAiBrowserComponent } from '@feidao/web/interfaces';
import update_pwd from '@feidao/web/atom/browser/msg/update-pwd';
import storage_clear from '@feidao/web/atom/browser/storage/clear';
import url_redirect from '@feidao/web/atom/browser/url/direct-to'
import user_loginout from '@feidao/web/atom/browser/msg/user-logout';
import fire from '@feidao/web/atom/browser/msg/fire';
import { send_msg as url, get_user_info } from './config';
import whoami from '@feidao/web/atom/browser/msg/whoami';
// import nodejs from '@feidao/web/atom/browser/msg/nodejs';
//修改密码
const actions = {
	cmpt_show(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		// 将已经输入的内容清空
		const inputs = fd.data.node.querySelectorAll('input[type="password"]');
		Array.from(inputs).forEach((i: HTMLInputElement) => {
			i.value = '';
		});
		// 将错误信息提示隐藏
		fd.data.node.querySelector('[data-feidao-zjc]').classList.remove('display-n')
	},
	close_cmpt(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		fd.data.node.querySelector('[data-feidao-zjc]').classList.add('display-n')
	},

	async upd_pwd(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		// 获取输入框的内容
		const current_pwd = fd.data.node.querySelector('[data-feidao-id="current_pwd"]') as HTMLInputElement;
		if (!current_pwd.value) {
			fire('zjc-000001', 'ec-001', 'fail', '请输入当前密码');
			return;
		}
		let new_pwd = fd.data.node.querySelector('[data-feidao-id="new_pwd"]') as HTMLInputElement;
		new_pwd.value.replace(/^[\W]{6,15}$/, '');
		// debugger
		if (!new_pwd.value) {
			fire('zjc-000001', 'ec-001', 'fail', '请输入新密码');
			return;
		}
		if (new_pwd.value.replace(/^[\w]{6,15}$/, '')) {
			fire('zjc-000001', 'ec-001', 'fail', '请正确输入密码');
			return;
		}

		const sure_pwd = fd.data.node.querySelector('[data-feidao-id="sure_pwd"]') as HTMLInputElement;
		if (!sure_pwd.value) {
			fire('zjc-000001', 'ec-001', 'fail', '请输入确认密码');
			return;
		}
		if (new_pwd.value !== sure_pwd.value) {
			fire('zjc-000001', 'ec-001', 'fail', '确认密码与新密码不一致');
			return;
		}
		if (new_pwd.value === current_pwd.value) {
			fire('zjc-000001', 'ec-001', 'fail', '新密码与旧密码一致');
			return;
		}

		try {
			const user = await whoami(get_user_info) as {
				productid: string;
				usercode: string;
				user_name: string;
			};
			const usercode = user.usercode;
			const productid = user.productid;
			const edit_type = 'normal';
			const id = user.usercode;
			const captcha = current_pwd.value;
			const password = new_pwd.value;

			const res = <{ msg: string, result: boolean }>await update_pwd(url, productid, edit_type, id, password, captcha, false, true)


			if (res.result == true) {
				// const q = {
				// 	operator: user.user_name,
				// 	module_name: '首页',
				// 	op_type: '修改',
				// 	op_cont: '"' + user.user_name + '"在「首页」在修改了密码',
				// 	op_time: new Date().getTime(),

				// }
				// console.log(q)
				// await nodejs(url, 'system_log_add', q, false, false);
				await user_loginout(url, systemid, usercode);
				await storage_clear('user');
				await storage_clear();
				url_redirect('login.html', {});
			} else {
				// 修改密码失败
				fire('zjc-000001', 'ec-001', 'fail', res.msg);
			}
		} catch (error) {
			fire('zjc-000001', 'ec-001', 'fail', '修改密码失败');
		}
	}
};

const s = {
	'cmpt_show': 'cmpt_show',
	'close_cmpt': 'close_cmpt',
	'upd_pwd': 'upd_pwd'
};

export default function main(url: string, query: any) {
	return init(id, s, actions, url, query, { spaceid, systemid, refsystemid });
}
